package cn.tedu.weibo.controller;

import cn.tedu.weibo.mapper.CommentMapper;
import cn.tedu.weibo.pojo.dto.CommentDTO;
import cn.tedu.weibo.pojo.entity.Comment;
import cn.tedu.weibo.pojo.entity.User;
import cn.tedu.weibo.pojo.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("/insert")
    public int addComment(@RequestBody CommentDTO commentDTO, HttpSession session){
        User user = (User)session.getAttribute("user");
        if (user==null){
            return 2;
        }
        Comment comment = new Comment();
        comment.setUserId(user.getId());
        comment.setContent(commentDTO.getContent());
        comment.setCreated(new Date());
        comment.setBlogId(commentDTO.getWeiboId());
        commentMapper.insert(comment);
        return 1;
    }

    @GetMapping("selectByWeiboId")
    public List<CommentVO> findIndexComment(Integer id){
        return commentMapper.findByBlogId(id);
    }
}
